
package huajiu.com.serviceImpl;

import huajiu.com.base.CdResult;
import huajiu.com.base.CommonConstant;
import huajiu.com.dao.UserDao;
import huajiu.com.domain.Record;
import huajiu.com.domain.User;
import huajiu.com.service.ApiService;
import huajiu.com.service.RecordService;
import huajiu.com.utlis.ImageUtil;
import huajiu.com.utlis.MD5Util;
import huajiu.com.utlis.NetHttpRrequest;
import huajiu.com.utlis.PayToolUtil;

import java.io.File;
import java.util.Date;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sun.glass.ui.Pixels.Format;

/**
 * @author ItBotPjq
 *2018年1月24日
 */
@Service
public class ApiServiceImpl implements ApiService {
	
	
	@Autowired
	private RecordService recordService;
	
	@Autowired
	private UserDao userDao;
	/* (non-Javadoc)
	 * @see huajiu.com.service.ApiService#getCorInterface(java.lang.String, java.lang.String)
	 */
	
	public static final String SAVE_PATH ="/mnt/images/";
	
	public static final String FORMAT = ".jpg";
	@Override
	public CdResult getCorInterface(String mid,String sign,String timestamp,String base64,String ext) {
		String localSign = createLocalSign(mid,timestamp);
		String imgpathString = produceImgpathStr();
		CdResult cdResult =null;
		if(localSign.equals(sign)){
			cdResult = NetHttpRrequest.recognizePhysique(base64, ext);
		}else {
			cdResult = CdResult.failCdResult();
		}
		ImageUtil.generateImage(base64, imgpathString);
		recordService.save(produceRecord(mid,cdResult,imgpathString));
		return cdResult;
				
	}
	/* (non-Javadoc)
	 * @see huajiu.com.service.ApiService#getTonInterface(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public CdResult getTonInterface(String mid, String sign, String timestamp,
			String base64, String ext) {
		String imgpathString = produceImgpathStr();
		String localSign = createLocalSign(mid,timestamp);
		CdResult cdResult =null;
		if(localSign.equals(sign)){
			cdResult= NetHttpRrequest.recognizeTongue(base64, ext);
		}else {
			cdResult = CdResult.failCdResult();
		}
		ImageUtil.generateImage(base64, imgpathString);
		recordService.save(produceRecord(mid,cdResult,imgpathString));
		return cdResult;
	}
	/* (non-Javadoc)
	 * @see huajiu.com.service.ApiService#getEmoInterface(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public CdResult getEmoInterface(String mid, String sign, String timestamp,
			String base64, String ext) {
		String imgpathString = produceImgpathStr();
		String localSign = createLocalSign(mid,timestamp);
		CdResult cdResult =null;
		if(localSign.equals(sign)){
			cdResult= NetHttpRrequest.emotion(base64, ext);
		}else {
			cdResult = CdResult.failCdResult();
		}
		ImageUtil.generateImage(base64, imgpathString);
		recordService.save(produceRecord(mid,cdResult,imgpathString));
		return cdResult;
	}
	/**
	 * 生产一个Record
	 * @param mid
	 * @param cdResult
	 * @return
	 */
	public Record produceRecord(String mid,CdResult cdResult,String imgpath){
		Record record = new Record();
		record.setMid(Integer.valueOf(mid));
		record.setRecord(getRecordStr(cdResult));
		record.setCreateTime(PayToolUtil.getCurrTime());
		record.setImgpath(produceImgpathStr());
		record.setImgpath(imgpath);
		return record;
	}
	/**
	 * 将结果转为json字符串
	 * @param cdResult
	 * @return
	 */
	public String  getRecordStr(CdResult cdResult){
		return JSONObject.fromObject(cdResult).toString();
	}
	/**
	 * 生成本地sign
	 * @param mid
	 * @param timestamp
	 * @return
	 */
	 public String createLocalSign(String mid,String timestamp){
	    	User user =userDao.selectUserByMid(mid);
			String md5key = user.getMd5Key();
			StringBuilder builder = new StringBuilder();
			builder.append(Integer.valueOf(mid));
			builder.append(md5key);
			builder.append(timestamp);
			String signString = builder.toString();
			String md5Sign = MD5Util.MD5Encode(signString, CommonConstant.CHARSET_NAME_UTF_8);
		    return	md5Sign.toLowerCase();
	    }
	 
	 public String produceImgpathStr(){
		 File fiel = new File(SAVE_PATH);
		 if(fiel.exists()){
			 fiel.mkdir();
		 }
		 String imgpath = SAVE_PATH + new Date().getTime()+FORMAT;
		 return imgpath;
	 }
}
